<template>
<div>
  <el-form ref="form" :model="form" label-width="100px" style="margin-top: 20px">
  <el-form-item label="视频上传">
    <el-upload
      class="upload-demo"
      drag
      multiple
      :show-file-list="true"
      action="http://localhost:9999/api/video/uploadAlyuVideo"
      :on-preview="handlePictureCardPreview"
      :on-remove="handleRemove"
      :on-success="handleAvatarSuccess"
      :before-upload="beforeAvatarUpload"
      :data="uploadHeader"
    >
      <i class="el-icon-upload"></i>
      <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
      <div class="el-upload__tip" slot="tip">只能上传avi/mp4文件，且不超过100M</div>
    </el-upload>
  </el-form-item>
  </el-form>
</div>
</template>

<script>
import { getToken } from '@/utils/auth'

export default {
  name: 'videoUpload',
  data(){
    return{
      dialogImageUrl:'',
      dialogVisible:false,
      //上传需要携带的数据
      uploadHeader:{"token":getToken()},
      form:{
        image:''
      }
    }
  },
  methods:{
    handleRemove(file, fileList) {
      console.log(file, fileList);
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url;
      this.dialogVisible = true;
    },
    /**
     * * 上传成功回调
     * * @param res *
     * @param file
     * */
    handleAvatarSuccess(res, file) {
      console.log(res)
      this.form.image = res.data
      // 强制重新渲染
      this.$forceUpdate()
    },
    /*** 上传校验 * @param file * @returns {boolean} */
    beforeAvatarUpload(file) {
      console.log(file.type)
      const isJPG = file.type === 'video/mp4'
      const isLt2M = file.size / 102400 / 102400 < 2
      if (!isJPG) {
        this.$message.error('上传视频只能是mp4格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传视频大小不能超过 20MB!')
      }
      return isJPG && isLt2M
    },
  }
}
</script>

<style lang="scss">
//用户头像
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9 !important;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}
.avatar-uploader .avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 250px;
  height: 250px;
  line-height: 250px;
  text-align: center;
}

.avatar-uploader img {
  width: 250px;
  height: 250px;
  display: block;
}
</style>
